//
//  URLConnection.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 19/02/12.
//  Copyright (c) 2012 Edenyorke. All rights reserved.
//

#import "URLConnection.h"
#import "MessageUtils.h"

@implementation URLConnection
@synthesize request,messageUtils;


-(URLConnection*) initWithIp{
    self = [super init];
    
    if ( self ) {
        messageUtils =  [[MessageUtils alloc] init];
        ipXkey = [messageUtils getValue:@"ipxkey"];
       
    }
    
    return self;
}

-(NSURL*) getURLForXml {
    NSString *urlData = [NSString stringWithFormat:@"%@/%@/%@", @"http:/",ipXkey, @"data.xml"];
    urlData = [urlData stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSURL *url = [[NSURL alloc] initWithString:urlData];
    return url;
}

-(NSURL*) getURLForLaunchGame:(NSString*) gameId{
    NSString *urlData = [NSString stringWithFormat:@"%@/%@/%@?%@", @"http:/",ipXkey, @"launchgame.sh",gameId];
    urlData = [urlData stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSURL *url = [[NSURL alloc] initWithString:urlData];
    return url;
}

-(UIImage*) checkImageForId: (NSString*) gameId{
    gameId = [gameId stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSString *urlData = [NSString stringWithFormat:@"%@/%@/%@/%@%@", @"http:/",ipXkey, @"covers",gameId,@".jpg"];
    NSLog(@"%@",urlData);
    NSURL *url = [[NSURL alloc] initWithString:urlData];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];     
    return image;
}

-(NSURL*) getURLXmlForId: (NSString*) gameId{
    gameId = [gameId stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSString *urlData = [NSString stringWithFormat:@"%@/%@/%@/%@%@", @"http:/",ipXkey, @"covers",gameId,@".xml"];
    NSLog(@"%@",urlData);
    NSURL *url = [[NSURL alloc] initWithString:urlData];
    return url;
}




@end
